package com.jcl.recruit.register.service.impl;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.corp.logic.SubcompanyLogic;
import com.jcl.recruit.corp.model.Subcompany;
import com.jcl.recruit.depart.logic.DepartmentLogic;
import com.jcl.recruit.org.model.Department;
import com.jcl.recruit.user.logic.UserRegisterLogic;
import com.jcl.recruit.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.jcl.recruit.register.service.JclRegiser;
import com.jcl.recruit.register.service.RegisterResult;
import com.jcl.recruit.register.service.RegisterVo;

import java.util.List;

/**
 * @ClassName AbstractJclRegisterImpl
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/14 11:36
 * @Version 1.0
 **/
@Component
public abstract class AbstractJclRegisterImpl implements JclRegiser {

    @Autowired
    private SubcompanyLogic subcompanyLogic;

    @Autowired
    private DepartmentLogic departmentLogic;

    @Autowired
    protected UserRegisterLogic userRegisterLogic;

    /* *
     * @Author yyliu
     * @Description 判断企业是否存在
     * @Date 17:38 2018/9/12
     * @Param [email]
     * @return com.example.model.Subcompany
     **/
    public Subcompany validateExist(String email, String name){
        if(StringUtils.hasLength(email)){
            email = email.substring(email.indexOf(StringUtils.EMAIL_SPLIT), email.length());
            List<Subcompany> subcompany = subcompanyLogic.findOneByEmail(email, name);
            if(CollectionUtils.isNotEmpty(subcompany)){
                return subcompany.get(0);
            }
            return null;
        }
        return null;
    }

    @Override
    public RegisterResult registerCorp(RegisterVo registerVo) {

        RegisterResult result = new RegisterResult();
        Subcompany subcompany = initSubcompany(registerVo);
        // 校验注册企业是否存在
        Subcompany tempCorp = validateExist(subcompany.getEmail(), subcompany.getFullname());
        if(null != tempCorp){
            result.setCode(RegisterResult.CORP_EXIST_CODE);
            return result;
        }
        // 完善信息
        Subcompany completeCorp = subcompanyLogic.completeCorp(subcompany);
        // 企业入库
        subcompanyLogic.add(completeCorp);
        // 初始化数据
        // 部门
        Department rootDepart = departmentLogic.addRoot(subcompany.getFullname(), completeCorp.getId(), completeCorp.getCode());

        // 初始人员信息
        User user = initUser(registerVo);
        user = userRegisterLogic.corpRegisterMappingUser(user, completeCorp, rootDepart.getId());

        // 发送消息
        if(registerVo.isSendEmail()){
            // ...
        }

        result.setSubcompany(completeCorp);
        result.setDepartment(rootDepart);
        result.setUser(user);
        return result;
    }

    private Subcompany initSubcompany(RegisterVo registerVo){
        Subcompany subcompany = new Subcompany();
        subcompany.setEmail(registerVo.getEmail());
        subcompany.setTelephone(registerVo.getTelephone());
        subcompany.setFullname(registerVo.getCorpName());
        return subcompany;
    }

    private User initUser(RegisterVo registerVo){
        User user = new User();
        user.setEmail(registerVo.getEmail());
        String userName = null;
        if(StringUtils.hasLength(registerVo.getUsername())){
            userName = registerVo.getUsername();
        }else{
            if(null != registerVo.getUserAccess()){
                userName = registerVo.getUserAccess().getNickname();
            }
        }
        user.setRealname(userName);
        user.setUsername(userName);
        return user;
    }
}
